class CustomShader {
    public static Init() {
        let old_drawTextureElements = egret.sys.drawTextureElements
        egret.sys.drawTextureElements = (renderContext: egret.sys.RenderContext, data: any, offset: number): number => {
            if (!window["GL"]) {
                window["GL"] = renderContext["context"]
            }

            let filter = data.filter
            if (filter && filter instanceof egret.CustomFilter) {
                // console.log("CustomShader:" + renderContext)
                // window["dumpObject"](renderContext)
                let webglrendercontext = renderContext;
                let gl = webglrendercontext["context"]
                let textures = filter["textures"]
                if (textures) {
                    for (const key in textures) {
                        if (Object.prototype.hasOwnProperty.call(textures, key)) {
                            gl.activeTexture(gl[key]);
                            let tex = textures[key]
                            // window["dumpObject"](tex)
                            try {
                                if (tex.$bitmapData) {
                                    gl.bindTexture(gl.TEXTURE_2D, webglrendercontext["getWebGLTexture"](tex.$bitmapData));
                                } else {
                                    gl.bindTexture(gl.TEXTURE_2D, tex);
                                }
                            } catch (e) {
                                console.error(e)
                            }
                        }
                    }
                }
            }
            return old_drawTextureElements(renderContext, data, offset)
        }
    }
}